import { AddPostionParam, AddTypeParam, EditPositionParam, EditTypeParam, GetTypeParam, TypeListType} from "@Tea/model"
import express, { Request, Response } from "express"
import { TypeManageCxt } from "../../context/TypeManageCxt"
import { checkError } from "../../utils/token"
import { useCheckAndResponse } from "../../utils/common"

const typeManage = express.Router()

typeManage.post("/typeList", async (req: Request, res: Response) => {
  const params = req.body as GetTypeParam
  try {
    const data = await TypeManageCxt.getInstance().getTypeList(params)
  
    setTimeout(() => {
      res.send({
        code: 1,
        msg: '查询成功',
        data
      })
    }, Math.random() * 1000)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg:checkError(ex)
    })
  }
})

typeManage.post("/addType", async (req: Request, res: Response) => {
  try {
    const params = req.body as AddTypeParam
    const result = await TypeManageCxt.getInstance().addType(params)
    
    useCheckAndResponse(res, result[0], '新增成功')
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

typeManage.post("/editType",async (req: Request, res: Response) => {
  try {
    const params = req.body as EditTypeParam
    const result = await TypeManageCxt.getInstance().editType(params)
    
    useCheckAndResponse(res, result[0], '编辑成功')
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

typeManage.delete("/delType", async (req: Request, res: Response) => {
  try {
    const typeId = req.query.typeId as any as TypeListType["typeId"]
    const result = await TypeManageCxt.getInstance().delType(typeId)
    
    useCheckAndResponse(res, result[0], "删除成功")
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
}) 

export default typeManage